// 自定义指令
import Vue from "vue";
import router from "../router/index";

Vue.directive("btnpermission", {
  inserted: function(el, binding) {
    const action = binding.value.action;
    const currentRight = router.currentRoute.meta.rules;
    if (currentRight) {
      if (currentRight.indexOf(action) == -1) {
        // 不具备权限
        const type = binding.value.effect;
        if (type === "disabled") {
          el.disabled = true;
          el.classList.add("is-disabled");
        } else {
          el.parentNode.removeChild(el);
        }
      }
    }
  }
});

Vue.directive("debounce", {
  inserted(el, binding) {
    el.addEventListener("click", () => {
      if (!el.disabled) {
        el.disabled = true;
        setTimeout(() => {
          el.disabled = false;
        }, binding.value || 3 * 1000);
      }
    });
  }
});

Vue.directive("htmlNo", {
  update(el, binding) {
    binding.value.action =
      typeof binding.value.action == "number"
        ? binding.value.action + ""
        : binding.value.action;
    if (
      binding.value.action == null ||
      binding.value.action == "" ||
      binding.value.action == "undefined"||
      binding.value.action == "null"
    ) {
      el.innerText = "-";
    } else {
      if (!binding.value.hasOwnProperty("unit")) {
        el.innerText = binding.value.action;
      } else {
        el.innerText = binding.value.action + binding.value.unit;
      }
    }
    // el.innerHTML = binding.value;
  }
});
